// pages/peccancyDetail/peccancyDetail.js
import config from "../../config.js";
const base = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {

    carNo: '', //车牌号
    carId: 0,
    wzList: [], //违章列表
    detail: {
      deduct: 0,
      forfeit: 30,
      service: 50
    },
    defaultKeeper: {
      name: '',
      header: '',
      houseKeeperId: null
    },
    select: {
      selectList: [],
      fen: 0,
      total: 0.0
    },
    isFirst: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var carId = options.id;
    if (wx.getStorageSync('user') != null) {
      that.setData({
        'defaultKeeper.name': wx.getStorageSync("user").housekeeper.name,
        'defaultKeeper.header': wx.getStorageSync("user").housekeeper.image,
        'defaultKeeper.houseKeeperId': wx.getStorageSync("user").housekeeper.keeperId
      })
    }

    that.setData({
      carId: carId
    })

  },
  loadData: function() {
    var that = this;
    wx.showLoading({
      title: "正在查询"
    })
    wx.request({
      url: base.carIllegalQuery,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: that.data.carId
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          that.setData({
            carNo: res.data.hphm,
            wzList: res.data.data
          })

          if (res.data.data.length == 0) {
            wx.showToast({
              title: "该车辆暂无违章信息",
              icon: "none"
            })
          }
        } else {
          wx.showToast({
            title: res.data.message,
            icon: "none"
          })

          that.setData({
            carNo: res.data.hphm,
            'detail.deduct': 1
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;

    if (!that.data.isFirst) {
      var houseKeeper = wx.getStorageSync("houseKeeper");
      if (houseKeeper) {
        this.setData({
          "defaultKeeper.name": houseKeeper.name,
          "defaultKeeper.houseKeeperId": houseKeeper.keeperId,
          "defaultKeeper.header": houseKeeper.image,
        })

        // console.log(this.data.mainInfo);
      }
    }
    this.loadData();
    that.setData({
      isFirst: false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  submit: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    var user = wx.getStorageSync("user");
    var houseKeeperId = that.data.defaultKeeper.houseKeeperId;
    var orderType = 3;
    var carId = that.data.carId;
    var orderFrom = 1;
    var time = that.data.date + " " + that.data.time;

    wx.request({
      url: base.carIllegalSubbmit,
      data: {
        carId: carId,
        orderId: -1,
        orderType: orderType,
        orderFrom: orderFrom,
        houseKeeperId: houseKeeperId,
        typeId: this.data.detail.deduct == 0 ? 1 : 2,
        selectIds: JSON.stringify(that.data.select.selectList)
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          wx.setStorageSync("orderId", res.data.data.oid);
          wx.setStorageSync("orderConfirmType", 1);
          var orderId = wx.getStorageSync("orderId");
          wx.navigateTo({
            url: "../orderSuccess/orderSuccess?id=" + orderId,
            complete: function() {
              wx.setStorageSync("orderConfirmType", null);
              wx.setStorageSync("orderId", null);
            }
          })
          // wx.showModal({
          //   title: "恭喜您",
          //   content: "预约成功，车管家将会在10分钟内通过电话与您沟通详情 ，请保持电话畅通！谢谢！",
          //   showCancel: false,
          //   confirmColor: "#137AF0",
          //   success: function() {

          //   }
          // })

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }

      },
      error: function() {
        wx.showToast({
          title: "请求失败,请联系管理员",
          icon: 'none',
          mask: true,
          duration: 2000
        })
      }
    })
  },
  changeKeeper: function() {
    var instance = this;
    wx.navigateTo({
      url: '../managerList/managerList',
    })
  },
  contact: function() {
    wx.makePhoneCall({
      phoneNumber: "400-9988960",
    })
  },
  selectItemAction: function(e) {
    var that = this;
    console.log(e);
    var selectList = e.detail.value;
    console.log(selectList);
    var total = 0;
    var fen = 0;

    for (var j = 0; j < selectList.length; j++) {
      for (var i = 0; i < that.data.wzList.length; i++) {
        var item = that.data.wzList[i];
        if (item.peccancyId == selectList[j]) {
          fen += item.fen;
          total += item.money;
        }
      }
    }

    //设置数据
    that.setData({
      'select.selectList': selectList,
      'select.fen': fen,
      'select.total': total
    })

    console.log(that.data);
  },
  lookFeeAction: function() {
    wx.navigateTo({
      url: '../orderSuccess/orderSuccess'
    })
  }
})